//
//  UIView+CustomAnimation.m
//  CoolMove
//
//  Created by CA on 15/9/10.
//  Copyright (c) 2015年 CA. All rights reserved.
//

#import "UIView+CustomAnimation.h"
#import <pop/POP.h>

@implementation UIView (CustomAnimation)

- (void)configureCornerRadiusbyRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
#pragma mark - Animation

#pragma mark - 隐现动画

- (void)performFadePopupAnimationWithHandleBlock:(HandleBlock)block
{
    POPBasicAnimation *opacityAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
    
    opacityAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    opacityAnim.duration = 0.3;
    opacityAnim.toValue = @0.0;
    opacityAnim.delegate = self;
    
    opacityAnim.name = @"AnimateOpacity";
    opacityAnim.completionBlock = ^(POPAnimation *anim, BOOL isFinish) {
        if (block) {
            block();
        }
        [self performPopUpAnimation];
    };
    
    [self.layer pop_addAnimation:opacityAnim forKey:@"AnimateOpacity"];
}

-(void)performPopUpAnimation
{
    [self pop_removeAllAnimations];
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    anim.springBounciness = 10;
    anim.springSpeed = 10;
    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
    
    POPBasicAnimation *opacityAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
    
    opacityAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    opacityAnim.duration = 0.5;
    opacityAnim.toValue = @1.0;
    
    [self.layer pop_addAnimation:anim forKey:@"AnimationScale"];
    [self.layer pop_addAnimation:opacityAnim forKey:@"AnimateOpacity"];
}

#pragma mark - 淡化

- (void)fadeUpAnimationWithDelay:(CGFloat)delay duration:(CGFloat)duration completionBlock:(HandleBlock)block
{
    self.alpha = 0.0f;
    [UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
        if (finished) {
            if (block != nil) {
                block();
            }
        }
    }];
}

#pragma mark - 淡出淡现，放大缩小
- (void)xwkFadeUpAnimationWithDelay:(CGFloat)delay duration:(CGFloat)duration  Scale:(CGFloat)scale completionBlock:(HandleBlock)block;
{
    [UIView animateWithDuration:duration/2.0 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(scale, scale);
    } completion:^(BOOL finished) {
        if (finished) {
            [UIView animateWithDuration:duration/2.0 animations:^{
                self.alpha = 0;
                self.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                if (finished) {
                    block();
                }
            }];
        }
        
    }];
}


@end
